<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Buyer;
use DB;


class LoginController extends Controller
{
   

    public function loginDo(Request $request)
    {
    	$rs['status'] = false;
        $username  = $request->username;
    	//$password  = $request->password;


        if(str_contains($request->username, '@')){
            $where['email'] = $username;    
        } else {
            $where['username']  = $username;
        }

        $where['type'] = 2;  // 刷手
        
    	$user = User::where($where)->first();
        //echo md5($password.$user->salt);
    	if($user) {
            if($user->status==1) {
                $rs['status']          = true;
                $rs['data']['token']   = $user->api_token;
                $rs['data']['buyer']   = $user->buyer;
                return response()->json($rs);
            }else {
                 $rs['msg'] = '账号审核中，请耐心等待';
            }
            return  response()->json($rs);
    	}
        $rs['msg'] = '用户不存在';
        return  response()->json($rs);

    }

    

    public function reg(Request $request)
    {
        $rs['status'] = false;
        $salt   = str_random(40);
        $where['username'] = $request->username;
        $where['type']     = 2;
        $hasUser = User::where($where)->first();
        if ($hasUser) {
            if($hasUser->status==1) {
                $rs['status']          = true;
                $rs['data']['token']   = $hasUser->api_token;
                $rs['data']['buyer']   = $hasUser->buyer;
                return response()->json($rs);
            }else{
                $rs['msg'] = '账号审核中，请耐心等待';
            }
            return  response()->json($rs);
        }
        
        if (strlen(trim($request->username)) != 11) {
            $rs['msg'] = '手机号码长度错误';
            return $rs;
        }

        if(!preg_match("/^1[34578]{1}\d{9}$/",$request->username)){            
            $rs['msg'] = '手机号码格式错误';  
            return $rs;
        }  

        // 没有此账号就新增注册
        $user = new User;
        $user->username = $request->username;
        //$user->email= $request->email;
        $user->type   = 2; //1 卖家 2买家
        $user->salt   = $salt;
        $user->status = 0;
        $user->api_token = str_random(60);
        //$user->password = md5($request->password.$salt);

        try {
            DB::beginTransaction();
            $user->save();
            $buyer['user_id']  = $user->id;
            $buyer['mobile']   = $request->username;
            $buyer['truename'] = $request->truename;
            $flag = Buyer::create($buyer);

            if($flag) {           
                

                DB::commit();
                // 需要审核之后才能登陆
                // $rs['status'] = true;
                // $rs['data']['token'] = $user->api_token;
                // $rs['data']['buyer'] = $user->buyer;
                $rs['msg'] = '注册已完成，请耐心等待账号审核';
                return response()->json($rs);
            } else {
                DB::rollBack();
            }
            
        } catch (\Exception $e) {
            DB::rollBack();
            $rs['msg'] = $e->getMessage();
            return   response()->json($rs);
            
        }
        

        $rs['msg'] = '注册失败';
        return $rs;
    }

    /**
     * 发送短信
     */
    public function sendmsg(Request $request)
    {
        

        $rs['status'] = false;
        $action  = 'login';
        if($request->action) {
            $action = $request->action;
        }

        $mobile = $request->mobile;

        // 注册和登陆时候要检测手机号是否存在 
        // 注册时已存在不能登陆 ，登陆时不存在不能登陆
        $checkRs = $this->checkStauts($mobile,2,$action);
        //dd($checRs);
        if(!$checkRs['status']) {
            $rs['msg'] = $checkRs['msg'];
            return $rs;
        }
        $result       = $this->sendsms($mobile);
        if($result['status']) {
            $rs['status'] = true;
            $rs['data']   = $result['data'];
            $rs['msg']    = '短信发送成功';
            return $rs;
            
        }
        $rs['msg'] = $result['msg'];
        return response()->json($rs);

    }

    public function checkAndrodVersion()
    {
        $rs['status'] = false;
        $flag = DB::table('systems')->first();
        if($flag) {
            $rs['status'] = true;
            $rs['data']   = $flag;
            return response()->json($rs);
        }
        return  response()->json($rs);


    }

    
}
